package com.mc.cloud.mall.categoryProduct.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.cloud.mall.categoryProduct.model.mapper.CategoryMapper;
import com.mc.cloud.mall.categoryProduct.model.pojo.Category;
import com.mc.cloud.mall.categoryProduct.model.vo.AddCategory;
import com.mc.cloud.mall.categoryProduct.model.vo.CategoryVO;
import com.mc.cloud.mall.common.exception.MallException;
import com.mc.cloud.mall.common.exception.MallExceptionEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CategoryServiceImpl
 * @Author MC
 * @Date 2020/11/4 10:07
 * @Version 1.0
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    
    @Autowired
    CategoryMapper categoryMapper;
    
    @Override
    public void addCategory(AddCategory addCategory){
        Category category = new Category();
        BeanUtils.copyProperties(addCategory,category);
        
        //查询是否存在这个分类
        Category categoryOld = categoryMapper.selectByName(addCategory.getName());
        if (categoryOld !=null){
            throw new MallException(MallExceptionEnum.NAME_EXISTED);
        }
    
        int i = categoryMapper.insertSelective(category);
        if (i ==0){
            throw new MallException(MallExceptionEnum.CREATE_FAILED);
        }
    }
    
    
    @Override
    public void updateCategory(Category updateCategory){
        //查询这个类目是否存在
        
        if (updateCategory.getName()!=null){
            Category categoryOld = categoryMapper.selectByName(updateCategory.getName());
            if (categoryOld!=null && !updateCategory.getId().equals(categoryOld.getId())){
                throw new MallException(MallExceptionEnum.NAME_EXISTED);
            }
        }
        int i = categoryMapper.updateByPrimaryKeySelective(updateCategory);
        if (i==0){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
        
    }
    
    
    @Override
    public void deleteCategory(Integer id){
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        if (categoryOld == null) {
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
        int i = categoryMapper.deleteByPrimaryKey(id);
        if (i==0){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }
    
    
    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer size){
        
        PageHelper.startPage(pageNum,size,"type,order_num");
        List<Category> categoryList = categoryMapper.selectList();
        PageInfo pageInfo = new PageInfo(categoryList);
        return pageInfo;
    
    }
    
    @Override
    @Cacheable(value = "listForCustom")
    public List<CategoryVO> listForCustom(Integer parentId){
        
        ArrayList<CategoryVO> categoryVOList = new ArrayList<>();
        recursivelyFindCategories(categoryVOList,parentId);
        return categoryVOList;
        
    }
    
    private void recursivelyFindCategories(List<CategoryVO> categoryVOList,Integer parentId){
        List<Category> categories = categoryMapper.selectCategoriesByParentId(parentId);
        if (categories!=null){
            for (Category category : categories) {
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category,categoryVO);
                categoryVOList.add(categoryVO);
                recursivelyFindCategories(categoryVO.getChildCategory(),categoryVO.getId());
            }
        }
    }
}
